package pATT.SAVER.DM;

import java.io.Serializable;
import java.util.Vector;

import pATT.SAVER.DM.suggestions.SAVERSuggestion;
import pATT.SAVER.DM.suggestions.SAVERSuggestion1;
import pATT.SAVER.DM.suggestions.SAVERSuggestion2;
import pATT.SAVER.DM.suggestions.SAVERSuggestion3;
import pATT.SAVER.DM.suggestions.SAVERSuggestion4;
import pATT.SAVER.DM.suggestions.SAVERSuggestion5;
import pATT.SAVER.courseStruct.Contenidos;
import pATT.bNEdit.base.RedBayes;
import pATT.bNEdit.inference.AlgoritmoInferencia;
import pATT.bNEdit.inference.BucketElimination;
import pATT.core.Utils;
import pATT.logger.Message;
import pATT.profile.BayesNetProfileComponent;

public class DMSAVERManager implements Serializable{

	private static final long serialVersionUID = 1L;

	private Vector<SAVERSuggestion> sugerencias = null;
	//sugerencia por default
	private SAVERSuggestion suggDefault = null;

	public DMSAVERManager() {
		sugerencias = new Vector<SAVERSuggestion>();
		initSuggestion();


	}

	/**
	 * inicializa las sugerencias
	 *
	 */
	private void initSuggestion() {
		suggDefault = new SAVERSuggestion();

		putSuggestion(new SAVERSuggestion1());
		putSuggestion(new SAVERSuggestion2());
		putSuggestion(new SAVERSuggestion3());
		putSuggestion(new SAVERSuggestion4());
		putSuggestion(new SAVERSuggestion5());
//		putSuggestion(new SAVERSuggestion6());

	}

	/**
	 * Agrega una SUGERENCIA
	 * @param suggestion
	 */
	private void putSuggestion(SAVERSuggestion suggestion) {
		sugerencias.addElement(suggestion);

	}
	/**
	 * Obtiene la sugeencia correcta para el usuario dado
	 * @param sit 
	 * 
	 * @param idUsuario
	 * @param net
	 * @return
	 */
	public String checkSuggestion(Vector sit, Contenidos contenidos, BayesNetProfileComponent net){
		//si no hay contenidos o no hay situaciones
		if(sit.isEmpty() || contenidos.isEmpty()){
			return suggDefault.toString();
		}
		String estilo = getNetStyle(net.getNet());
		for (int i = 0; i < sugerencias.size(); i++) {
			SAVERSuggestion sugg = sugerencias.elementAt(i);
			if(sugg.checkDecision(sit, contenidos, estilo)){
				return sugg.toString();
			}
		}
		return suggDefault.toString();
	}

	/**
	 * Retorna cual es el estilo de aprendizaje de la red
	 * @return
	 */
	protected String getNetStyle(RedBayes net){
		AlgoritmoInferencia ai = new BucketElimination();
		double[] array = new double[4];

		array[0] = ai.query(net, Message.PERCEPCION, Message.INTUITIVO);
		array[1] = ai.query(net, Message.PERCEPCION, Message.SENSITIVO);
		array[2] = ai.query(net, Message.COMPRENSION, Message.SECUENCIAL);
		array[3] = ai.query(net, Message.COMPRENSION, Message.GLOBAL);

		int index = Utils.maxIndex(array);

		if(index == 0){
			return Message.INTUITIVO;
		}
		if(index == 1){
			return Message.SENSITIVO;
		}
		if(index == 2){
			return Message.SECUENCIAL;
		}
		return Message.GLOBAL;

	}

}
